package com.yuufa.test.ex;


public class Ex1 {
    
    public static void main(String[] args) {
        
        String result = toString(45, 4);
        System.out.println(result);
    }
    
    final static char[] digits = {
            '0' , '1' , '2' , '3' , '4' , '5' ,
            '6' , '7' , '8' , '9' , 'a' , 'b' ,
            'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
            'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
            'o' , 'p' , 'q' , 'r' , 's' , 't' ,
            'u' , 'v' , 'w' , 'x' , 'y' , 'z'
            };
    
    public static String toString(int i, int shift) {
        
        char[] buf = new char[32];
        int charPos = 32;
        int radix = 1 << shift;
        int mask = radix - 1;

        do {
            buf[--charPos] = digits[i & mask];
            i >>>= shift;
        } while (i != 0);
        
        return new String(buf, charPos, (32 - charPos));
    }
    
    public static void toBinary(int num, int radix) {
        if(num < radix) {
            System.out.print(digits[num]);
        } else {
            toBinary(num/radix, radix);
            System.out.print(digits[num % radix]);
        }
    }
    
    public static void test() {
        int signBit = getTheMostBit(444);
        
        System.out.println("the sign bit = " + signBit);
    }

    public static int getTheMostBit(int num) {
        
        int c = 0;
        
        while(num != 1) {
            num = num >> 1;
            c++;
        }
        
        return c;
    }
}
